Reference

class template
<functional>

std::negate

template <class T> struct negate;
Negative function object class
Unary function object class whose call returns the result of negating its argument (as returned by the unary operator -).

Negating a value returns the same value with the opposite sign.

Generically, function objects are instances of a class with member function operator() defined. This member function allows the object to be used with the same syntax as a function call.

It is defined with the same behavior as:

1
2
3
template <class T> struct negate : unary_function <T,T> {
 T operator() (const T& x) const {return -x;}
};
1
2
3
4
5
template <class T> struct negate {
 T operator() (const T& x) const {return -x;}
 typedef T argument_type;
 typedef T result_type;
};

Objects of this class can be used on standard algorithms such as transform .

Template parameters

T
Type of the argument and return type of the functional call.
The type shall support the operation (unary operator-).

Member types

member typedefinitionnotes
argument_typeTType of the argument in member operator()
result_typeboolType returned by member operator()

Member functions

T operator() (const T& x)
Member function returning the negation of its argument (-x).

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
// negate example
#include <iostream> // std::cout
#include <functional> // std::negate
#include <algorithm> // std::transform
int main () {
 int numbers[]={10,-20,30,-40,50};
 std::transform (numbers, numbers+5, numbers, std::negate<int>());
 for (int i=0; i<5; i++)
 std::cout << numbers[i] << ' ';
 std::cout << '\n';
 return 0;
}

Output:

-10 20 -30 40 -50


See also

plus
Addition function object class (class template)
minus
Subtraction function object class (class template)
multiplies
Multiplication function object class (class template)
divides
Division function object class (class template)
modulus
Modulus function object class (class template)
equal_to
Function object class for equality comparison (class template)

AltStyle によって変換されたページ (->オリジナル) /